package part18;

import java.io.File;
import java.io.FilenameFilter;
import java.util.*;
import java.util.regex.Pattern;

/**
 * @author wcj
 * @Date 2021/6/2 15:58
 * @Version 1.0
 */
public class Part184 {

    public static File[] local(File dir, final String regex) {
        return dir.listFiles(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(regex);

            @Override
            public boolean accept(File dir, String name) {
                return pattern.matcher(name).matches();
            }
        });
    }

    /**
     * 通过文件路径可以构造一个File
     *
     * @param filePath
     * @param regex
     * @return
     */
    public static File[] local(String filePath, final String regex) {
        return local(new File(filePath), regex);
    }

    public static class TreeInfo implements Iterable<File> {
        private List<File> files = new ArrayList<>();
        private List<File> dirs = new ArrayList<>();

        @Override
        public Iterator<File> iterator() {
            return files.iterator();
        }

        public void addAll(TreeInfo info) {
            files.addAll(info.files);
            dirs.addAll(info.dirs);
        }

        @Override
        public String toString() {
            return "dirs:" + PPrint.pFormat(dirs) + "\n\nfiles:" + PPrint.pFormat(files);
        }
    }

    public static TreeInfo walk(String start, String regex) {
        return recurDirs(new File(start), regex);
    }

    public static TreeInfo walk(File start, String regex) {
        return recurDirs(start, regex);
    }

    public static TreeInfo walk(File start) {
        return recurDirs(start, ".*");
    }

    public static TreeInfo walk(String start) {
        return recurDirs(new File(start), ".*");
    }

    public static TreeInfo recurDirs(File startDir, String regex) {
        TreeInfo res = new TreeInfo();
        for (File file : startDir.listFiles()) {
            if (file.isDirectory()) {
                res.dirs.add(file);
                res.addAll(recurDirs(file, regex));
            } else {
                if (file.getName().matches(regex)) {
                    res.files.add(file);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(walk("./src"));
    }
}

/**
 * 打印工具类：格式化打印输出
 */
class PPrint {
    public static String pFormat(Collection<?> c) {
        if (c.size() == 0) {
            return "[]";
        }
        StringBuilder res = new StringBuilder("[");
        for (Object o : c) {
            if (c.size() != 1) {
                res.append("\n  ");
            }
            res.append(o);
        }
        if (c.size() != 1) {
            res.append("\n");
        }
        res.append("]");
        return res.toString();
    }

    public static void pPrint(Collection<?> c) {
        System.out.println(pFormat(c));
    }

    public static void pPrint(Object[] c) {
        System.out.println(pFormat(Arrays.asList(c)));
    }
}
